﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class DefAccountSetItem : System.Web.UI.Page
    {
        SetsItemBiz setsItemBiz = new SetsItemBiz();

        DefDicBiz defineDictionary = new DefDicBiz();
        string id = "0";//By Update
        string aset = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["Aset"] != null)
                aset = Request.Params["Aset"].ToString();
            else
            {
                JSUtility.Alert(this, "调用方式失败！缺少Aset参数！");
                return;
            }
            if (Request.Params["ID"] != null)
            {
                id = Request.Params["ID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                defineDictionary.InitDropDownList("Wage_Sets", ddlAccountSet, aset);
                defineDictionary.InitXMLDropDownList(ddlSourceFrom,"WageInterface.xml","");

               lstItems.DataSource=setsItemBiz.GetItemList(aset);
               lstItems.DataTextField = "ItemName";
               lstItems.DataValueField = "ItemName";// "WageItem";
               lstItems.DataBind();
                if (id != "0")
                {                   
                    BindData(id);
                }
               
            }
        }
        private void BindData(string id)
        {
            DataTable dt = defineDictionary.GetOneInfo("Wage_SetsItem", id);
            if (dt.Rows.Count > 0)
            {
                txtItemName.Text = dt.Rows[0]["ItemName"].ToString();
                hidItemCode.Value = dt.Rows[0]["WageItem"].ToString();
                txtAbsIndex.Text = dt.Rows[0]["AbsIndex"].ToString();
                txtInitValue.Text = dt.Rows[0]["InitValue"].ToString();
                txtCnFormula.Text = dt.Rows[0]["CnFormula"].ToString();
                string itemType = dt.Rows[0]["ItemType"].ToString();
                defineDictionary.InitDropDownList(ddlItemType, itemType);
                ShowPanel(itemType);
                defineDictionary.InitDropDownList(ddlSourceFrom, dt.Rows[0]["SourceFrom"].ToString());
      
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string itemName = txtItemName.Text.Trim();
            if (string.IsNullOrWhiteSpace(itemName))
            {
                JSUtility.Alert(this, "名称不能为空！");
                return;
            }

            string initValue = txtInitValue.Text.Trim();
            string absIndex = txtAbsIndex.Text.Trim();//
            if (!Utils.RegexRule.IsInt(absIndex))
            {
                JSUtility.Alert(this, "索引一定要填写数字！");
                return;
            }
            string itemType = ddlItemType.SelectedItem.Text.Trim();
            string itemCode = setsItemBiz.GetFirstItem(aset);//自动计算 找最小的，没有使用的一个代码

           string cnFormula= txtCnFormula.Text.Trim();
          
            
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SetsID", aset);            
            sdList.Add("ItemName", itemName);
            sdList.Add("InitValue", initValue);
            sdList.Add("ItemType", itemType);
            sdList.Add("AbsIndex", absIndex);
            sdList.Add("WageItem", itemCode);
            if (!string.IsNullOrWhiteSpace(cnFormula))//求出实际公式
            {
                string enFormula = setsItemBiz.GetEnFormula(aset, cnFormula);               
                sdList.Add("CnFormula", cnFormula);
                sdList.Add("EnFormula", enFormula);
            }
            sdList.Add("SourceFrom", ddlSourceFrom.SelectedValue);

            defineDictionary.Insert("Wage_SetsItem", sdList);

          
            JSUtility.AlertAndRedirect("增加项目成功", "SetsItemAdd.aspx?Aset=" + aset);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
             string itemName = txtItemName.Text.Trim();
            if (string.IsNullOrWhiteSpace(itemName))
            {
                JSUtility.Alert(this, "名称不能为空！");
                return;
            }

            string initValue = txtInitValue.Text.Trim();
            string absIndex = txtAbsIndex.Text.Trim();//
            if (!Utils.RegexRule.IsInt(absIndex))
            {
                JSUtility.Alert(this, "索引一定要填写数字！");
                return;
            }
            string itemType = ddlItemType.SelectedItem.Text.Trim();
            string itemCode = hidItemCode.Value;

           string cnFormula= txtCnFormula.Text.Trim();
          
            
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("ID", id);          
            sdList.Add("SetsID", aset);            
            sdList.Add("ItemName", itemName);
            sdList.Add("InitValue", initValue);
            sdList.Add("ItemType", itemType);
            sdList.Add("AbsIndex", absIndex);
            sdList.Add("WageItem", itemCode);
            if (!string.IsNullOrWhiteSpace(cnFormula))//求出实际公式
            {
                string enFormula = setsItemBiz.GetEnFormula(aset, cnFormula);
                sdList.Add("CnFormula", cnFormula);
                sdList.Add("EnFormula", enFormula);
            }           
            sdList.Add("SourceFrom", ddlSourceFrom.SelectedValue);

            if (defineDictionary.Update("Wage_SetsItem", sdList))
            {
                //修改所有员工基本工资帐套中 该项目的初始值
                if (ckbInit.Checked == true)
                {
                    new EmpItemBiz().UpdateAllInfo(itemCode, initValue, aset);
                }
                JSUtility.AlertAndRedirect("修改成功", "SetsItemAdd.aspx?Aset=" + aset);
            }
            else
                JSUtility.Alert(this, "修改失败！");
            
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string itemCode = hidItemCode.Value;
            defineDictionary.Delete("Wage_SetsItem", new string[] { "ID", id, "N" });
            new EmpItemBiz().UpdateAllInfo(itemCode, "0", aset);
            JSUtility.Alert(this,"成功删除");
        }

        protected void ddlItemType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string itemType=ddlItemType.SelectedValue;
            ShowPanel(itemType);

        }

        private void ShowPanel(string itemType)
        {
            if (itemType == "输入项")
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
                 
            }
            else
            {
                Panel2.Visible = true;
                Panel1.Visible = false;
            }
        }
    }
}